<template>
  <el-dialog :close-on-click-modal="false" v-drag :title="$t('添加')" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" width="490px" top="5%" @close="close">
    <el-form ref="form" :model="record" :inline="true" label-width="120px" :rules="rules" label-position="left" style="margin: 0 25px">
      <el-form-item :label="$t('功能码')" prop="commond">
        <el-input v-emoji v-model="record.commond" ref="commond" class="addTypeForm" size="small"></el-input>
      </el-form-item>
      <el-form-item :label="$t('功能码描述')" prop="describe">
        <el-input v-emoji v-model="record.describe" ref="describe" placeholder="NULL" class="addTypeForm" size="small"></el-input>
      </el-form-item>
      <el-form-item :label="$t('类型')" prop="type">
        <el-select v-model="record.type" class="addTypeForm" ref="recordType" size="small">
          <el-option v-for="(value, key) in cipTypeList" :key="key" :label="$t(value)" :value="key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('点名')" prop="name">
        <el-input v-emoji v-model="record.name" ref="names" placeholder="ANY" class="addTypeForm" size="small"></el-input>
        <el-tooltip class="item" effect="light" :content="$t('多个点名以逗号(,)隔开')" placement="top">
          <i class="el-icon-warning-outline" style="font-size: 16px"></i>
        </el-tooltip>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmitThree">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddCloseThree">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import ruleArray from '../../ruleArray'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'AddConfigDialog',
  props: ['addConfigRows'],
  data () {
    var validateCommond = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value)) || !this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 1 || Number(value) > 255) {
        callback(new Error(this.$t('输入范围1~255')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        commond: '',
        describe: '',
        type: 'other',
        name: ''
      },
      recordCopy: {
        commond: '',
        describe: '',
        type: 'other',
        name: ''
      },
      cipTypeList: ruleArray.cipTypeList,
      rules: {
        commond: { required: false, validator: validateCommond, trigger: 'blur' }
      }
    }
  },
  methods: {
    init () {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['names', 'commond', 'describe']
        const selectData = ['recordType']
        inputClass(this.$refs, data)
        selectClass(this.$refs, selectData)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          var formData = JSON.parse(JSON.stringify(this.record))
          if (!formData.describe) {
            formData.describe = 'null'
          }
          if (!formData.name) {
            formData.name = 'any'
          }
          this.$emit('addConfigRows', Object.assign({}, formData))
          this.close()
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
<style lang="scss">
.addTypeForm {
  width: 250px !important;
}
</style>
